#include <windows.h>
#include "CDX3DTransform.h"
#include "CReleaseMacro.h"
#include <d2d1helper.h>
using namespace D2D1;
CDX3DTransform::CDX3DTransform()
{
	m_pMatrixStack = NULL;
	D3DXCreateMatrixStack(0, &m_pMatrixStack);
	m_pMatrixStack->LoadIdentity();
}
CDX3DTransform::~CDX3DTransform()
{
	SAFE_RELEASE(m_pMatrixStack);
}
D3DXMATRIX CDX3DTransform::GetTransform()
{
	D3DXMATRIX ret = *(m_pMatrixStack->GetTop());
	return ret;
}
HRESULT CDX3DTransform::SetTransform(const D3DXMATRIX* mat)
{
	return m_pMatrixStack->LoadMatrix(mat);
}
HRESULT CDX3DTransform::Translate(float x, float y, float z)
{
	return m_pMatrixStack->Translate(x, y, z);
}
HRESULT CDX3DTransform::TranslateLocal(float x, float y, float z)
{
	return m_pMatrixStack->TranslateLocal(x, y, z);
}
HRESULT CDX3DTransform::RotateAxis(CONST D3DXVECTOR3 * pV, FLOAT angle)
{
	return m_pMatrixStack->RotateAxis(pV, angle);
}
HRESULT CDX3DTransform::RotateAxisLocal(CONST D3DXVECTOR3 * pV, FLOAT angle)
{
	return m_pMatrixStack->RotateAxisLocal(pV, angle);
}
HRESULT CDX3DTransform::Scale(float sx, float sy, float sz)
{
	return m_pMatrixStack->Scale(sx, sy, sz);
}
HRESULT CDX3DTransform::ScaleLocal(float sx, float sy, float sz)
{
	return m_pMatrixStack->ScaleLocal(sx, sy, sz);
}
HRESULT CDX3DTransform::MultMatrix(const D3DXMATRIX *pMat)
{
	return m_pMatrixStack->MultMatrix(pMat);
}
HRESULT CDX3DTransform::MultMatrixLocal(const D3DXMATRIX *pMat)
{
	return m_pMatrixStack->MultMatrixLocal(pMat);
}
HRESULT CDX3DTransform::LoadIdentity()
{
	return m_pMatrixStack->LoadIdentity();
}
HRESULT CDX3DTransform::PushMatrix()
{
	return m_pMatrixStack->Push();
}
HRESULT CDX3DTransform::PopMatrix()
{
	return m_pMatrixStack->Pop();
}
Matrix3x2F CDX3DTransform::GetTransform2D()
{
	Matrix3x2F matD2D;
	D3DXMATRIX curmat = GetTransform();
	matD2D._11 = curmat._11;
	matD2D._12 = curmat._12;
	matD2D._21 = curmat._21;
	matD2D._22 = curmat._22;
	matD2D._31 = curmat._41;
	matD2D._32 = curmat._42;
	return matD2D;
}

HRESULT CDX3DTransform::Transform3DPoint(D3DXVECTOR3& outVec, const D3DXVECTOR3& inVec)
{
	D3DXMATRIX matT = GetTransform();
	D3DXVec3TransformCoord(&outVec, &inVec, &matT);
	return S_OK;
}
HRESULT CDX3DTransform::Transform3DPoints(D3DXVECTOR3* outVec, const D3DXVECTOR3* inVec, UINT nPoints)
{
	D3DXMATRIX matT = GetTransform();
	for (UINT i =0; i < nPoints; i++)
	{
		D3DXVec3TransformCoord(&(outVec[i]), &(inVec[i]), &matT);
	}
	
	return S_OK;
}
HRESULT CDX3DTransform::InvTransform3DPoint(D3DXVECTOR3& outVec, const D3DXVECTOR3& inVec)
{
	D3DXMATRIX matT = GetTransform();
	D3DXMATRIX matInvT;
	D3DXMatrixInverse(&matInvT, NULL, &matT);
	D3DXVec3TransformCoord(&outVec, &inVec, &matInvT);
	return S_OK;
}
HRESULT CDX3DTransform::InvTransform3DPoints(D3DXVECTOR3* outVec, const D3DXVECTOR3* inVec, UINT nPoints)
{
	D3DXMATRIX matT = GetTransform();
	D3DXMATRIX matInvT;
	D3DXMatrixInverse(&matInvT, NULL, &matT);
	for (UINT i =0; i < nPoints; i++)
	{
		D3DXVec3TransformCoord(&(outVec[i]), &(inVec[i]), &matInvT);
	}
	return S_OK;
}